from flask import Blueprint,jsonify,g,json
from flask_restful import Api,Resource,marshal,reqparse
from common.model_fields.comment_fields import comments_fields
from models import db
from common.utils.login_utils import login_required
comment_bp = Blueprint('comment',__name__,url_prefix='/comments')
from models.user import *
api = Api(comment_bp)
class CommentResource(Resource):
    @login_required
    def get(self,nid):
        """
        获取所有评论
        1.接收前端传来的文章id
        2.获取这个文章的所有的评论
            1.判断评论有没有被回复
            2.若有回复，获取回复的评论内容

        """
        comments = Comment.query.filter_by(article_id=nid).all()
        if not comments:
            # 说明文章没有评论
            return jsonify(message='获取评论成功', data=[],code=200)
        # 有评论, 把有序字段转化成字典
        comments_dict = json.loads(json.dumps(marshal(comments, comments_fields)))
        print('评论的字典数据>>', comments_dict)
        for comment in comments_dict:
            # 获取评论的用户id, 获取用户信息
            user = UserModel.query.filter_by(uid=comment['user_id']).first()
            comment['user_info'] = {
                'uid':user.uid,
                'uname':user.uname,
                'profile_photo': user.profile_photo
            }
            # 判断文章是否有回复
            is_reply_comment = comment['parent_id']
            # 回复评论的数据
            child_comment = []
            if is_reply_comment:
                # 有回复,
                # TODO 获取回复的评论内容及用户信息
                comments = Comment.query.filter_by(cmid=comment['parent_id']).all()
                comments_dict = json.loads(json.dumps(marshal(comments, comments_fields)))
                for comment in comments_dict:
                    # 获取评论的用户id, 获取用户信息
                    user = UserModel.query.filter_by(uid=comment['user_id']).first()
                    comment['user_info'] = {
                        'uid': user.uid,
                        'uname': user.uname,
                        'profile_photo': user.profile_photo
                    }
            else:
                # 没有回复
                comment['child_comment'] = child_comment
        # 返回包含子评论的所有数据
        print('包含用户信息的评论内容>>', comments_dict)
        return jsonify(message='获取评论成功', code=200, data=comments_dict)

    @login_required
    def post(self,nid):
        parser = reqparse.RequestParser()
        parser.add_argument('content')
        parser.add_argument('parent_id')
        parser.add_argument('article_id')
        args = parser.parse_args()
        content = args.get('content')
        parent_id = args.get('parent_id')
        article_id = args.get('article_id')
        if parent_id:
            user_id = g.user_id
            comment = Comment(user_id=user_id,partent_id = parent_id,content=content)
            db.session.add(comment)
            db.session.commit()

        else:
            user_id = g.user_id
            comment = Comment(user_id=user_id,article_id=article_id,content=content)
            db.session.add(comment)
            db.session.commit()
        return jsonify(message='发布成功',code=200)

api.add_resource(CommentResource,'/all/<int:nid>')

